package br.com.android.productivecenter;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Serializable;
import java.security.PublicKey;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import br.com.android.R;
import br.com.android.modelo.Dialogs;
import br.com.android.persistencia.manipularDados;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;

public class ProductiveCenterActivity extends Activity {
    /** Called when the activity is first created. */
	
	public manipularDados persistencia;
	
    private static final String[] ferramentas = {"Google Docs", "Google Calendar"};
    ArrayAdapter<String> ferramentaEscolhida;
    Spinner spinnerFerramentas;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        Bundle extras = getIntent().getExtras();
        
        if(extras == null){
        
	        try {
				persistencia = new manipularDados(this);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }			
		else persistencia = getIntent().getExtras().getParcelable("nelson");
		       
        CarregaTelaPrincipal();
    }
    
    public void CarregaTelaPrincipal() {
    	
    	setContentView(R.layout.main);
    	
    	ferramentaEscolhida = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ferramentas);
    	spinnerFerramentas = (Spinner) findViewById(R.id.spinnerOpcoes);
    	spinnerFerramentas.setAdapter(ferramentaEscolhida);
    	
    	Button bttela2 = (Button) findViewById(R.id.entrarB);
    	bttela2.setOnClickListener(new View.OnClickListener(){

    		public void onClick(View arg0) {
    			
    			EditText login = (EditText)findViewById(R.id.loginEt);
    			String email = login.getText().toString();
    			
    			EditText senha = (EditText)findViewById(R.id.senhaF);
    			String s = senha.getText().toString();
    			
                             
                if(email.equals("") || s.equals(""))
                	Dialogs.imprimirMensagem(ProductiveCenterActivity.this, "Informações incorretas", "Entre novamente com as informações");
                else	{
                	
                	int res = email.indexOf("gmail.com");
                	
                	if(res > 0){
		    			switch (spinnerFerramentas.getSelectedItemPosition()) {
		    				case 0: Intent i = new Intent(ProductiveCenterActivity.this,GoogleDocumentsActivity.class);
		    						i.putExtra("nelson", persistencia);
		    						i.putExtra("userName", email);
		    						startActivity(i);
		    						break;
		    				case 1: Intent i2 = new Intent(ProductiveCenterActivity.this,GoogleCalendarActivity.class); 
									i2.putExtra("nelson", persistencia);
		    						i2.putExtra("userName", email);
		    						startActivity(i2);
									break;
		    			}
	                }
	                else Dialogs.imprimirMensagem(ProductiveCenterActivity.this, "Email incorreto", "Por favor digite um email válido!");
                }
    		}
    	});
    }      	    
 }